using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	#endregion
	public class TinTucRepository
	{
        public List<TinTuc> SelectNewest()
        {
            try
            {
                return DataContext.Instance.TinTuc.OrderByDescending(n => n.NgayDang).Take(10).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("TinTuc - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("TinTuc - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<TinTuc>();
            }
        }

		public List<TinTuc> SelectAll()
		{
			try
			{
				return DataContext.Instance.TinTuc.OrderByDescending(n => n.NgayDang).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<TinTuc>();
			}
		}

		public TinTuc SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.TinTuc.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return null;
			}
		}

		public List<TinTuc> SelectPaging(int page, int pageSize)
		{
			try
			{
                var lstItem = DataContext.Instance.TinTuc.OrderByDescending(n => n.NgayDang).ToList();
				if (pageSize > 0)
				{
					int startIndex, itemCount;
					startIndex = (page - 1) * pageSize;
					itemCount = pageSize;
					if (startIndex + itemCount > lstItem.Count)
						itemCount = lstItem.Count - startIndex;
					lstItem = lstItem.GetRange(startIndex, itemCount);
				}
				return lstItem;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - SelectPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - SelectPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<TinTuc>();
			}
		}

		public bool Insert(TinTuc obj)
		{
			try
			{
				DataContext.Instance.TinTuc.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.TinTuc.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.TinTuc.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			try
			{
				return DataContext.Instance.TinTuc.Count();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return -1;
			}
		}

		public int GetTotalItemByIDLoaiTinTuc(int idloaitintuc)
		{
			try
			{
				return (from p in DataContext.Instance.TinTuc where p.IDLoaiTinTuc == idloaitintuc select p).Count();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - GetTotalItemByIDLoaiTinTuc: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - GetTotalItemByIDLoaiTinTuc: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return -1;
			}
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.NumTinTucPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByIDLoaiTinTuc(int idloaitintuc)
		{
			try
			{
				int RecordCount = GetTotalItemByIDLoaiTinTuc(idloaitintuc);
				int PageSize = WebConfiguration.NumTinTucPerPage;
				return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - GetTotalPageByIDLoaiTinTuc: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - GetTotalPageByIDLoaiTinTuc: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return -1;
			}
		}


		public List<TinTuc> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.TinTuc where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<TinTuc>();
			}
		}

		public List<TinTuc> SelectByIDLoaiTinTuc(int idloaitintuc)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.TinTuc where item.IDLoaiTinTuc == idloaitintuc select item).OrderByDescending(n => n.NgayDang).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("TinTuc - SelectByIDLoaiTinTuc: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("TinTuc - SelectByIDLoaiTinTuc: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<TinTuc>();
			}
		}

	}
}